package com.itheima.d1_exception;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age)  {
        //方法默认的运行时异常处理方式（没有书写任何异常处理代码）： 异常抛出调用者

        //校验数据合法性
        if (age < 0 || age > 50) {
            //不合法的年龄 : 抛出一个异常 (把异常交给调用者处理)
            throw  new AgeIllegalException("非法年龄数据!");
            //System.out.println("");
            //细节：throw后面不能书写任何代码
        }
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
